/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs.admin;

import daos.CategoryDAO;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Category;
import util.Common;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class CategoryFormEdit extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            String actionName = pageContext.getRequest().getParameter(WebConst.ACTION);
            if (actionName.equalsIgnoreCase(WebConst.EDIT)) {
                int categoryId = Integer.parseInt(pageContext.getRequest().getParameter("categoryid"));
                Category category = CategoryDAO.singleton().select(categoryId);
                if (category == null && actionName.equalsIgnoreCase(WebConst.EDIT)) {
                    return SKIP_BODY;
                }
                writer.println("<form action='AdminCategoryServlet?action=update&categoryid="+categoryId+"' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Name:</td>");
                writer.println("<td>" + Common.createTextbox("txtName", "txtName", category.getName(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnUpdate", "btnUpdate", "Update", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            } else if (actionName.equalsIgnoreCase(WebConst.NEW)) {                
                writer.println("<form action='AdminCategoryServlet?action=create' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Name:</td>");
                writer.println("<td>" + Common.createTextbox("txtName", "txtName", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnCreate", "btnCreate", "Create", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            }
            
        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
